"""
功能介绍：读取接口类型，返回数据
导包
创建类
创建实例属性
接收excel表参数，传入
根据接口类型进行判断
返回接口结果
"""

# 导包
import logging

import requests
from 高级版接口第一周.common.log import log

# 创建类
class confighttp():
    # 创建实例属性
    def __init__(self,interfaceUrl,method,value):
        # 接收excel表参数，传入
        self.interfaceUrl=interfaceUrl
        self.method=method
        self.value=value
        # 创建方法
    def run3(self):
        if self.method.lower()=='get':
            return self.__get()

        elif self.method.lower()=='post':
            return self.__post()

        elif self.method.lower()=='put':
            return self.__put()

        elif self.method.lower()=='delete':
            return self.__delete()

        else:
            print('未找到，请检查')
        # 根据接口类型进行判断
    def __get(self):
        # 返回接口结果
        try:
            re=requests.get(url=self.interfaceUrl,params=eval(self.value))
            logging.info(f'接口返回:{re.text}')
        except Exception as msg:
            print('get接口请求失败,请检查',msg)
        else:
            return re

    def __post(self):
        # 返回接口结果
        try:
            re = requests.post(url=self.interfaceUrl, data=eval(self.value))
            logging.info(f'接口返回:{re.text}')
        except Exception as msg:
            print('post接口请求失败,请检查' , msg)
        else:
            return re

    def __put(self):
        # 返回接口结果
        try:
            re = requests.put(url=self.interfaceUrl,data=eval(self.value))
            logging.info(f'接口返回:{re.text}')
        except Exception as msg:
            print('put接口请求失败,请检查',msg)
        else:
            return re

    def __delete(self):
        # 返回接口结果
        try:
            re = requests.put(url=self.interfaceUrl, data=eval(self.value))
            logging.info(f'接口返回:{re.text}')
        except Exception as msg:
            print('delete接口请求失败,请检查 ',msg)
        else:
            return re

if __name__ == '__main__':
    url = "https://www.wanandroid.com/user/login"
    method = 'post'
    value = "{'username': 'liangchao', 'password': '123456'}"
    http1=confighttp(url,method,value,)
    wq=http1.run3()
    print(wq.text)
    print(wq.status_code)



